#include "TriangleList.h"
#include "Vertex.h"



void TriangleList::init(ID3D10Device *device, float scale)
{
	md3dDevice = device;
	//mNumVertices = 8;
	//mNumIndices = 12;

	// create vertex buffer
	Vertex vertices[] = {
		{D3DXVECTOR3(-1.0f, +0.0f, -1.0f), WHITE},
		{D3DXVECTOR3(+1.0f, +0.0f, -1.0f), BLACK},
		{D3DXVECTOR3(+1.0f, +0.0f, +1.0f), RED},
		{D3DXVECTOR3(-1.0f, +0.0f, +1.0f), GREEN},
		{D3DXVECTOR3(+0.0f, +1.0f, +0.0f), YELLOW}
	};
	mNumVertices = _countof(vertices);

	// scale the box
	for (DWORD i=0; i<mNumVertices; ++i)
		vertices[i].pos *= scale;
	
	D3D10_BUFFER_DESC vbd;
	vbd.Usage = D3D10_USAGE_IMMUTABLE;
	vbd.ByteWidth = sizeof(Vertex) * mNumVertices;
	vbd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
	vbd.CPUAccessFlags = 0;
	vbd.MiscFlags = 0;
	D3D10_SUBRESOURCE_DATA vinitData;
	vinitData.pSysMem = vertices;
	HR( md3dDevice->CreateBuffer(&vbd, &vinitData, &mVB) );

	// create the index buffer
	DWORD indices[] = {
		1, 3, 0,
		2, 3, 1,
		3, 4, 0,
		2, 4, 3,
		1, 4, 2,
		0, 4, 1
	};
	mNumIndices = _countof(indices);

	D3D10_BUFFER_DESC ibd;
	ibd.Usage = D3D10_USAGE_IMMUTABLE;
	//ibd.ByteWidth = sizeof(DWORD) * mNumFaces * 3;
	ibd.ByteWidth = sizeof(DWORD) * mNumIndices;
	ibd.BindFlags = D3D10_BIND_INDEX_BUFFER;
	ibd.CPUAccessFlags = 0;
	ibd.MiscFlags = 0;
	D3D10_SUBRESOURCE_DATA initData;
	initData.pSysMem = indices;
	HR( md3dDevice->CreateBuffer(&ibd, &initData, &mIB) );
}

void TriangleList::draw()
{
	
	// restore default states
	float blendFactors[] = {0.0f, 0.0f, 0.0f, 0.0f};
	md3dDevice->OMSetDepthStencilState(0, 0);
	md3dDevice->OMSetBlendState(0, blendFactors, 0xffffffff);
	//md3dDevice->IASetInputLayout(mVertexLayout);
	md3dDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

	UINT stride = sizeof(Vertex);
	UINT offset = 0;
	md3dDevice->IASetVertexBuffers(0, 1, &mVB, &stride, &offset);
	md3dDevice->IASetIndexBuffer(mIB, DXGI_FORMAT_R32_UINT, 0);
	md3dDevice->DrawIndexed(mNumIndices, 0, 0);
}
